package com.sumer.psychology.space.infrastructure.messaging.rabbitmq;

import com.sumer.psychology.common.port.adapter.messaging.Exchanges;
import com.sumer.psychology.common.port.adapter.messaging.rabbitmq.ExchangeListener;
import com.sumer.psychology.space.infrastructure.persistence.mongo.MongoMessageRepository;

/**
 * 为空间消息监听评论创建
 * <p/>
 * User: zhenguang.zhu
 * Date: 2014/11/5
 * Time: 11:15
 */
public class CommentMessageCreatedEventListener extends ExchangeListener {
    private static final String COMMENT_TYPE = "com.sumer.psychology.comment.domain.model.CommentCreatedEvent";
    private static final String COMMENT_SCORE_TYPE = "com.sumer.psychology.comment.domain.model.ScoredCommentCreatedEvent";

    private MongoMessageRepository mongoCommentMessageRepository;

    public CommentMessageCreatedEventListener(MongoMessageRepository mongoCommentMessageRepository) {
        this.mongoCommentMessageRepository = mongoCommentMessageRepository;
    }

    @Override
    protected String exchangeName() {
        return Exchanges.DEFAULT_EXCHANGE_NAME;
    }

    @Override
    protected void filteredDispatch(String aType, String aTextMessage) {
        mongoCommentMessageRepository().add(aTextMessage);
    }

    @Override
    protected String[] listensTo() {
        return new String[]{COMMENT_TYPE, COMMENT_SCORE_TYPE};
    }

    private MongoMessageRepository mongoCommentMessageRepository() {
        return mongoCommentMessageRepository;
    }
}
